//环境监测
function exposureLineChart(){
    var date = [];
    var today = [];
    var total = [];

    var envirChart = echarts.init(document.getElementById("lineChart"));

    var envirOption = {
        title: {
            text: '曝光量统计'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data:['每日曝光量','总曝光量']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data : date
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name:'每日曝光量',
                type:'line',
                stack: '总量',
                data : today
            },
            {
                name:'总曝光量',
                type:'line',
                stack: '总量',
                data:total
            }
        ]
    };

    //ajax异步请求数据
    $.ajax({
        async : true,
        type: 'get',
        url: '../../exposure/amount/12',//请求数据的地址
        //url: 'envir.json',//请求数据的地址
        dataType: "json",        //返回数据形式为json
        success: function (result) {
            console.info(result);
            var data = result.object;
            //请求成功时执行该函数内容，result即为服务器返回的json对象
            for( i in data) {
                date[i] = data[i].exposureDate;
                today[i] = data[i].todayAmount;
                total[i] = data[i].totalAmount;
            }
            envirChart.setOption(envirOption);//重新新加载数据
        },
        error: function (errorMsg) {
            //请求失败时执行该函数
            alert("图表请求数据失败!!");
        }
    });
}

function exposurePieChart() {
    var pieChart1 = echarts.init(document.getElementById("pieChart1"));
    var age = [];
    age.push('20以下');
    age.push('20-40');
    age.push('40-60');
    age.push('60-80');
    age.push('80以上');
    var ageSelected = {};
    ageSelected['20以下'] = 1;
    ageSelected['20-40'] = 1;
    ageSelected['40-60'] = 1;
    ageSelected['60-80'] = 1;
    ageSelected['80以上'] = 1;

    var ageSeriesData = [];

    var pieOption1 = {
        title : {
            text: '年龄统计',
            subtext: '纯属虚构',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 20,
            bottom: 20,
            data: age,
            selected:ageSelected
        },
        series : [
            {
                name: '年龄',
                type: 'pie',
                radius : '55%',
                center: ['40%', '50%'],
                data: ageSeriesData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };




    var pieChart2 = echarts.init(document.getElementById("pieChart2"));
    var sex = [];
    sex.push('男性');
    sex.push('女性');
    var sexSelected = {};
    sexSelected['男性'] = 1;
    sexSelected['女性'] = 1;
    var sexSeriesData = [];

    var pieOption2 = {
        title : {
            text: '性别统计',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 20,
            bottom: 20,
            data: sex,
            selected:sexSelected
        },
        series : [
            {
                name: '性别',
                type: 'pie',
                radius : '55%',
                center: ['40%', '50%'],
                data: sexSeriesData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };






    var pieChart3 = echarts.init(document.getElementById("pieChart3"));
    var marriage = [];
    marriage.push('已婚');
    marriage.push('未婚');
    var marriageSelected = {};
    marriageSelected['已婚'] = 1;
    marriageSelected['未婚'] = 1;
    var marriageSeriesData = [];

    var pieOption3 = {
        title : {
            text: '婚姻状况统计',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 20,
            bottom: 20,
            data: marriage,
            selected:marriageSelected
        },
        series : [
            {
                name: '性别',
                type: 'pie',
                radius : '55%',
                center: ['40%', '50%'],
                data: marriageSeriesData,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };






    //ajax异步请求数据
    $.ajax({
        async : true,
        type: 'get',
        url: '../../exposure/comparison/12',//请求数据的地址
        dataType: "json",        //返回数据形式为json
        success: function (result) {
            console.info(result);
            var data = result.object;
            ageSeriesData.push(
                {name: '20以下', value: data.ageANum},
                {name: '20-40', value: data.ageBNum},
                {name: '40-60', value: data.ageCNum},
                {name: '60-80', value: data.ageDNum},
                {name: '80以上', value: data.ageENum});
            pieChart1.setOption(pieOption1);//重新新加载数据

            sexSeriesData.push(
                {name: '男性', value: data.sexANum},
                {name: '女性', value: data.sexBNum});
            pieChart2.setOption(pieOption2);//重新新加载数据

            marriageSeriesData.push(
                {name: '已婚', value: data.marriageANum},
                {name: '未婚', value: data.marriageBNum});
            pieChart3.setOption(pieOption3);//重新新加载数据

        },

        error: function (errorMsg) {
            alert("图表请求数据失败!!");
        }
    });

}

setInterval(exposureLineChart, 10000);
setInterval(exposurePieChart, 10000);
